const app = getApp();
Page({
  data: {
    list: {
      price: 0
    },
    withdrawal_price:0,
    type:true,
    weiChoose:true,
    zhiChoose:false,
    zhiList:false
  },
  wei(){
    var that=this
    if (that.data.weiChoose==true){
      that.setData({
        weiChoose: false,
        zhiChoose:true,
        zhiList: true
      })
    }else{
      that.setData({
        weiChoose: true,
        zhiChoose:false,
        zhiList:false
      })
    }
   
  },
  zhi(){
    var that=this
    if (that.data.zhiChoose == true) {
      that.setData({
        weiChoose: true,
        zhiChoose: false,
        zhiList: false
      })
    } else {
      that.setData({
        weiChoose: false,
        zhiChoose: true,
        zhiList: true
      })
    }
  },

  onLoad: function (options) {
    const that = this;
    if (options.type === 'recharge'){
      that.setData({
        type: true
      })
      wx.setNavigationBarTitle({
        title: '充值',
      })
    }else{
      that.setData({
        type: false
      })
      wx.setNavigationBarTitle({
        title: '提现',
      })
    }
    app.util.request({
      url: 'Index/Index/getBalance',
      data: {
        openid: app.globalData.userInfo.openid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data === null || res.data.length == 0) {
          console.log('暂无数据');
        } else {
          that.setData({
            list: res.data
          })
        }
      }
    })
  },

  bindPrice:function(e){
    var that = this;
    that.setData({
      withdrawal_price:e.detail.value
    })
  },
  formSubmitMoney:function(e){
    const that = this;
    const withdrawal_price = that.data.withdrawal_price;
    if (that.data.type === true){
      if (!(/(^[1-9]\d*$)/.test(withdrawal_price))) {
        wx.showToast({
          title: '充值金额为正整数',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      app.util.request({
        url: 'Index/Index/recharge',
        data: {
          buyers_openid: app.globalData.userInfo.openid,
          total_price: withdrawal_price
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          const data = res.data.data;
          app.startpay(data);
        }
      })
    }else{
      if (!(/(^[1-9]\d*$)/.test(withdrawal_price))) {
        wx.showToast({
          title: '提现金额为正整数',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      if (withdrawal_price > that.data.list.price){
        wx.showToast({
          title: '提现金额不能大于余额金额',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    }
    if (e.detail.formId !== "the formId is a mock one"){
      var data = {
        openid: app.globalData.userInfo.openid,
        form_id: e.detail.formId
      };
      app.setFromId(data);
    }
  },

  allData:function(e){
    const that = this;
    that.setData({
      withdrawal_price: that.data.list.price
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})